import java.util.*;
import java.text.*;
import java.io.*;

public class CalLogic
{
    private final static String DATE_FORMAT = "MM/dd/yyyy";
    private final static String CAL_DATE_FORMAT = "yyyyMMdd";
    private final static String TIME_FORMAT = "hh:mm a";
    private final static String CAL_TIME_FORMAT = "HHmm"; 

    public CalLogic()
    {
    }

    public void validDate(String date) throws Exception
    {
	DateFormat df = new SimpleDateFormat(DATE_FORMAT);
	df.setLenient(false);

	df.parse(date);
    }

    public String convertDate(String date) throws Exception
    {
	DateFormat original = new SimpleDateFormat(DATE_FORMAT);
	DateFormat convert = new SimpleDateFormat(CAL_DATE_FORMAT);

	Date caldate = original.parse(date);

	return convert.format(caldate);
    }

    public void validTime(String time) throws Exception
    {
	DateFormat df = new SimpleDateFormat(TIME_FORMAT);
	df.setLenient(false);

	df.parse(time);
    }

    public String convertTime(String time) throws Exception
    {
	DateFormat original = new SimpleDateFormat(TIME_FORMAT);
	DateFormat convert = new SimpleDateFormat(CAL_TIME_FORMAT);

	Date caltime = original.parse(time);

	return convert.format(caltime) + "00";
    }

    public String getTimeZone()
    {
	DateFormat myDate = new SimpleDateFormat();

	return myDate.getTimeZone().getID();
    }

    public void toICSFile(StoreInput input)
    {
	String filename = input.getEventName();

	PrintWriter outputstream = null;
	try
	{
	    outputstream = new PrintWriter(filename);
	}
	catch(Exception e)
	{
	    System.out.println("Could not open file " + filename);
	}

	outputstream.println("BEGIN:VCALENDAR");
	outputstream.println("VERSION:2.0");
	outputstream.println("BEGIN:VEVENT");
	outputstream.println("CLASS:" + input.getPrivacy());
	outputstream.println("LOCATION:" + input.getLocation());
	outputstream.println("GEO:" + input.getLatitude() + ";" + input.getLongitude());
	outputstream.println("PRIORITY:" + input.getPriority());
	outputstream.println("SUMMARY:" + input.getSummary());
	outputstream.println("DTSTART;TZID=" + input.getTimezone() + ":" + 
	    input.getFromDate() + "T" + 
	    input.getFromTime() + "Z");
	outputstream.println("DTEND;TZID=" + input.getTimezone() + ":" + 
	    input.getToDate() + "T" + 
	    input.getToTime() + "Z");
	outputstream.println("END:VEVENT");
	outputstream.println("END:VCALENDAR");

	outputstream.close();
    }
}
